package cn.org.dian.easycommunicate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.TextView;
import cn.org.dian.easycommunicate.model.DataCenter;
import cn.org.dian.easycommunicate.model.Term;
import cn.org.dian.easycommunicate.model.Theme;
import cn.org.dian.easycommunicate.util.Constants;
import cn.org.dian.easycommunicate.util.Utilities;

/**
 * Display all terms under a specific theme using a ListView
 * 
 * @author LiuGen
 * @date 2011-1-21
 */
public class ThemeActivity extends Activity {

    private static final String TAG = "ThemeActivity";

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS,
                R.layout.progress);
        setContentView(R.layout.theme_activity);

        Intent passedIntent = getIntent();

        int themeIndicator = passedIntent.getIntExtra("theme_indicator", 0);
        int languageIndicator = passedIntent.getIntExtra("language_indicator",
                0);
        Log.d(TAG, "themeIndicator:" + themeIndicator + " languageIndicator: "
                + languageIndicator);

        setTitle(Theme.getAllThemeNames().get(themeIndicator));

        prepareDisplayData(languageIndicator, themeIndicator);

        // prepareTTS();
    }

    private void prepareDisplayData(final int languageIndicator,
            int themeIndicator) {
        // get data
        List<HashMap<String, Object>> contentList = new ArrayList<HashMap<String, Object>>();
        Theme currenTheme = DataCenter.getTheme(themeIndicator);
        HashMap<String, Term> allTerms = currenTheme.getAllTermsUnderTheme();
        String language = Term.getAllLanguageNames().get(languageIndicator);
        for (String termName : allTerms.keySet()) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("text_chinese", termName);
            map.put("text_foreign", allTerms.get(termName).getAllLanguages()
                    .get(language));
            // map.put("language_indicator", languageIndicator);
            contentList.add(map);
        }

        SimpleAdapter adapter = new SimpleAdapter(ThemeActivity.this,
                contentList, R.layout.theme_activity_list_item, new String[] {
                        "text_chinese", "text_foreign", "button_speak" },
                new int[] { R.id.text_chinese, R.id.text_foreign,
                        R.id.button_speak });
        ListView lv = (ListView) findViewById(R.id.theme_content_list);

        // customize a ViewBinder in order to add onClickListener for each
        // ImageButton
        adapter.setViewBinder(new ViewBinder() {

            @Override
            public boolean setViewValue(View view, Object data,
                    String textRepresentation) {
                int viewId = view.getId();
                if (viewId == R.id.text_chinese || viewId == R.id.text_foreign) {
                    // for TextView, do the normal action
                    ((TextView) view).setText((String) data);
                    return true;
                } else if (viewId == R.id.button_speak) {
                    // add onClickListener for each ImageButton
                    ((ImageButton) view)
                            .setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // get foreign text for speak
                                    TextView foreignTextView = (TextView) ((LinearLayout) ((RelativeLayout) v
                                            .getParent()).getChildAt(1))
                                            .getChildAt(1);
                                    Utilities.playLocalFile(
                                            Constants
                                                    .languageIndicatorToEnum(languageIndicator),
                                            (String) foreignTextView.getText());
                                    // asyncSpeak(languageIndicator, (String)
                                    // foreignTextView.getText());
                                }

                            });
                    return true;
                }
                return false;

            }

        });
        lv.setAdapter(adapter);

        // when items in ListView is clicked, goto TermActivity to display
        // detailed info about a term including all support languages
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // view is mapped to R.layout.theme_activity_list_item
                Log.d(TAG, "click: " + position);
                TextView termNameTextView = (TextView) ((LinearLayout) ((RelativeLayout) view)
                        .getChildAt(1)).getChildAt(0);
                String termName = (String) termNameTextView.getText();
                Intent intent = new Intent(ThemeActivity.this,
                        TermActivity.class);
                intent.putExtra("term_name", termName);
                startActivity(intent);
            }

        });

    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return Utilities.onCreateOptionsMenu(this, menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return Utilities.onOptionsItemSelected(this, item);
    }
}
